package validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import command.BasicInfoForm;

@Component
public class BasicInfoFormValidator implements Validator {
	
	@Override
	public boolean supports(Class<?> clazz) {
		return BasicInfoForm.class.isAssignableFrom(clazz);
	}
	
	@Override
	public void validate(Object target, Errors errors) {
		BasicInfoForm form = (BasicInfoForm) target;
		if (form.getName().trim().isEmpty()) {
			errors.rejectValue("name", "empty");
		} else if (form.getName().length() > 50) {
			errors.rejectValue("name", "tooLong");
		}
	}
}
